package com.pan.anchor.impl;

import com.github.kwhat.jnativehook.GlobalScreen;
import com.github.kwhat.jnativehook.NativeHookException;
import com.github.kwhat.jnativehook.keyboard.NativeKeyEvent;
import com.github.kwhat.jnativehook.keyboard.NativeKeyListener;
import com.pan.anchor.MyAnchor;
import com.pan.listener.MyListener;
import com.pan.listener.impl.SerializationSavePressedListener;

public class GlobalScreenAnchor implements MyAnchor {

    public GlobalScreenAnchor(){
        try {
            GlobalScreen.registerNativeHook();
        } catch (NativeHookException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void addListener(MyListener listener) {
        GlobalScreen.addNativeKeyListener(new NativeKeyListener() {
            @Override
            public void nativeKeyPressed(NativeKeyEvent nativeEvent) {
                listener.pressed(NativeKeyEvent.getKeyText(nativeEvent.getKeyCode()));
            }
            @Override
            public void nativeKeyReleased(NativeKeyEvent nativeEvent) {
                listener.released(NativeKeyEvent.getKeyText(nativeEvent.getKeyCode()));
            }
        });
    }
}
